Imports System
Imports System.Drawing.Printing
Imports System.IO
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Windows.Forms
Imports System.Xml

Imports Microsoft.Win32

Namespace gtalkNotepad

	Public Class Form1
		Inherits System.Windows.Forms.Form

		Private _emailIsOn As Boolean = True
		Private _chatIsOn As Boolean = True
		Private _currentContact As Contact
		Private _isComposing As Boolean = False
		Private _mailCount As Integer
		Private _nl As String = System.Environment.NewLine()
		Private _lastChatTextChange As Date

		Private _textChanged As Boolean

		Private _chatBuffer As String = ""
		Private _chatStart As Int32 = -1
		Private _chatEnd As Int32 = -1

		Private _selectedTextBox As textBoxes
		Private _filePath As String = "Untitled"

		Private _settings As New Settings()
		Private _chat As Chat

		Delegate Sub SetTextCallback(ByVal [text] As String)

#Region " Constants "

		Dim WTS_CONSOLE_CONNECT As Int32 = 1
		Dim WTS_CONSOLE_DISCONNECT As Int32 = 2
		Dim WTS_REMOTE_CONNECT As Int32 = 3
		Dim WTS_REMOTE_DISCONNECT As Int32 = 4
		Dim WTS_SESSION_LOGON As Int32 = 5
		Dim WTS_SESSION_LOGOFF As Int32 = 6
		Dim WTS_SESSION_LOCK As Int32 = 7
		Dim WTS_SESSION_UNLOCK As Int32 = 8
		Dim WTS_SESSION_REMOTE_CONTROL As Int32 = 9

		Private Const CHAT_INPUT_HEIGHT As Integer = 13
		Private Const NOTIFY_FOR_THIS_SESSION As Int32 = &H0
		Private Const WM_DESTROY As Int32 = &H2
		Private Const WM_ACTIVATEAPP As Int32 = &H1C
		Private Const _WIN32_WINNT As Int32 = &H501
		Private Const WM_WTSSESSION_CHANGE As Int32 = &H2B1
#End Region

#Region " DLL Imports "
		<DllImport("Wtsapi32.dll")> Private Shared Function WTSUnRegisterSessionNotification(ByVal hWnd As IntPtr) As Boolean
		End Function

		'The WTSRegisterSessionNotification function registers the specified
		'window to receive session-change notifications.
		<DllImport("Wtsapi32.dll")> Private Shared Function WTSRegisterSessionNotification(ByVal hWnd As IntPtr, ByVal dwFlags As Int32) As Boolean
		End Function

		'The PostQuitMessage function indicates to the system that a thread
		'has made a request to quit. The PostQuitMessage function is typically used in
		'response to a WM_DESTROY message.
		<DllImport("user32.dll")> Private Shared Sub PostQuitMessage(ByVal nExitCode As Int32)
		End Sub

		'The OpenIcon function restores a minimized (iconic) window to its
		'previous size and previous position. The OpenIcon function then activates the window.
		<DllImport("user32.dll")> _
		Private Shared Function OpenIcon(ByVal hwnd As IntPtr) As Boolean
		End Function

		'The SetForegroundWindow function puts the thread that created the
		'specified window in the foreground and then activates the window.
		<DllImport("user32.dll")> Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As Boolean
		End Function
#End Region

#Region " WndProc Override, Handle Create/Destroyed Override "
		<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
		  Protected Overrides Sub WndProc(ByRef m As Message)
			Select Case (m.Msg)
				Case WM_WTSSESSION_CHANGE

					Select Case (m.WParam.ToInt32)
						Case WTS_SESSION_LOCK
							If Not _chat Is Nothing AndAlso _chat.IsLoggedIn AndAlso _chat.Status = Contact.ContactStatus.Active Then
								_chat.SetStatus(Contact.ContactStatus.Idle)
								Debug.Print(DateTime.Now().ToString() & " Idle")
							End If
						Case WTS_SESSION_UNLOCK
							If Not _chat Is Nothing AndAlso _chat.IsLoggedIn AndAlso _chat.Status = Contact.ContactStatus.Idle Then
								_chat.SetStatus(Contact.ContactStatus.Active)
								Debug.Print(DateTime.Now().ToString() & " Active")
							End If
					End Select

			End Select
			MyBase.WndProc(m)
		End Sub

		Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)

			'The WTSRegisterSessionNotification function registers the specified
			'window to receive session-change notifications.

			WTSRegisterSessionNotification(Me.Handle, NOTIFY_FOR_THIS_SESSION)

			MyBase.OnHandleCreated(e)

		End Sub
		'
		Protected Overrides Sub OnHandleDestroyed(ByVal e As System.EventArgs)

			WTSUnRegisterSessionNotification(Me.Handle)
			PostQuitMessage(0)

		End Sub
#End Region

#Region " Form Components "

		Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
		Friend WithEvents mnu_showmail As System.Windows.Forms.MenuItem
		Friend WithEvents mnu_showtext As System.Windows.Forms.MenuItem

		Private components As System.ComponentModel.IContainer
		Private WithEvents mnuAbout As System.Windows.Forms.MenuItem
		Private WithEvents mnuSetBackCol As System.Windows.Forms.MenuItem
		Private menuItem7 As System.Windows.Forms.MenuItem
		Private menuItem6 As System.Windows.Forms.MenuItem
		Private WithEvents mnuSetFontColor As System.Windows.Forms.MenuItem
		Private colorDialog1 As System.Windows.Forms.ColorDialog
		Private fontDialog1 As System.Windows.Forms.FontDialog
		Private WithEvents mnuSetFont As System.Windows.Forms.MenuItem
		Private WithEvents mnuWordWrap As System.Windows.Forms.MenuItem
		Private WithEvents MnuTimeDate As System.Windows.Forms.MenuItem
		Private WithEvents mnuSelectAll As System.Windows.Forms.MenuItem
		Private menuItem5 As System.Windows.Forms.MenuItem
		Private WithEvents mnuDelete As System.Windows.Forms.MenuItem
		Private WithEvents mnuCut As System.Windows.Forms.MenuItem
		Private menuItem2 As System.Windows.Forms.MenuItem
		Private WithEvents mnuUndo As System.Windows.Forms.MenuItem
		Private pageSetupDialog1 As System.Windows.Forms.PageSetupDialog
		Private printDialog1 As System.Windows.Forms.PrintDialog
		Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
		Private openFileDialog1 As System.Windows.Forms.OpenFileDialog
		Private WithEvents mnuExit As System.Windows.Forms.MenuItem
		Private menuItem12 As System.Windows.Forms.MenuItem
		Private WithEvents mnuPrint As System.Windows.Forms.MenuItem
		Private WithEvents mnuPageSetUp As System.Windows.Forms.MenuItem
		Private menuItem9 As System.Windows.Forms.MenuItem
		Private WithEvents textBoxNormal As System.Windows.Forms.TextBox
		Friend WithEvents textBoxEmail As System.Windows.Forms.TextBox
		Private WithEvents mnuPaste As System.Windows.Forms.MenuItem
		Private WithEvents mnuCopy As System.Windows.Forms.MenuItem
		Private mnuEdit As System.Windows.Forms.MenuItem
		Private WithEvents mnuSaveAs As System.Windows.Forms.MenuItem
		Private WithEvents mnuSave As System.Windows.Forms.MenuItem
		Private WithEvents mnuOpen As System.Windows.Forms.MenuItem
		Private WithEvents mnuNew As System.Windows.Forms.MenuItem
		Private mnuFile As System.Windows.Forms.MenuItem
		Private mainMenu1 As System.Windows.Forms.MainMenu
		Friend WithEvents mnu_showChat As System.Windows.Forms.MenuItem
		Friend WithEvents textBoxChat As System.Windows.Forms.TextBox
		Friend WithEvents chatInput As System.Windows.Forms.TextBox
		Friend WithEvents timerChat As System.Windows.Forms.Timer
		Friend WithEvents mnu_Settings As System.Windows.Forms.MenuItem
		Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
		Friend WithEvents StatusStripMain As System.Windows.Forms.StatusStrip
		Friend WithEvents sslCurrentChat As System.Windows.Forms.ToolStripStatusLabel



		Public Enum textBoxes
			normal = 0
			gmail = 1
			chat = 2
		End Enum
#End Region

#Region " Constructor, Initialization, Dispose "

		Public Overloads Shared Sub Main(ByVal args() As String)
			Application.Run(New Form1)
		End Sub	'Main 

		Public Sub New()
			InitializeComponent()

			'chatInput.Dock = DockStyle.Top
			textBoxNormal.Dock = DockStyle.Fill
			textBoxChat.Dock = DockStyle.Fill
			textBoxChat.AllowDrop = True
			textBoxNormal.AllowDrop = True
			textBoxEmail.Dock = DockStyle.Fill

			With chatInput
				.Height = CHAT_INPUT_HEIGHT
				.Top = textBoxNormal.Top + 2
				.Width = Me.Width - 35
				.Left = textBoxNormal.Left + 3 '+ 50
				.Visible = False
			End With

			_mailCount = 0

			'TODO: Replace with Settings class.
			GetSettings() 'Get Default Setting
			'showMail()
		End Sub	'New

		Protected Overloads Sub Dispose(ByVal disposing As Boolean)
			If disposing Then
				If Not (components Is Nothing) Then
					components.Dispose()
				End If
			End If
			MyBase.Dispose(disposing)
		End Sub	'Dispose

		Private Sub InitializeComponent()
			Me.components = New System.ComponentModel.Container
			Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
			Me.mnuPaste = New System.Windows.Forms.MenuItem
			Me.mnuSetFontColor = New System.Windows.Forms.MenuItem
			Me.MnuTimeDate = New System.Windows.Forms.MenuItem
			Me.colorDialog1 = New System.Windows.Forms.ColorDialog
			Me.mnuSaveAs = New System.Windows.Forms.MenuItem
			Me.mnuSetBackCol = New System.Windows.Forms.MenuItem
			Me.mnuNew = New System.Windows.Forms.MenuItem
			Me.mnuAbout = New System.Windows.Forms.MenuItem
			Me.pageSetupDialog1 = New System.Windows.Forms.PageSetupDialog
			Me.mnuCopy = New System.Windows.Forms.MenuItem
			Me.mnuExit = New System.Windows.Forms.MenuItem
			Me.mnuUndo = New System.Windows.Forms.MenuItem
			Me.menuItem12 = New System.Windows.Forms.MenuItem
			Me.mnuPrint = New System.Windows.Forms.MenuItem
			Me.mnuFile = New System.Windows.Forms.MenuItem
			Me.mnuOpen = New System.Windows.Forms.MenuItem
			Me.mnuSave = New System.Windows.Forms.MenuItem
			Me.menuItem9 = New System.Windows.Forms.MenuItem
			Me.mnu_Settings = New System.Windows.Forms.MenuItem
			Me.MenuItem4 = New System.Windows.Forms.MenuItem
			Me.mnuPageSetUp = New System.Windows.Forms.MenuItem
			Me.mnuWordWrap = New System.Windows.Forms.MenuItem
			Me.mnuCut = New System.Windows.Forms.MenuItem
			Me.menuItem7 = New System.Windows.Forms.MenuItem
			Me.menuItem6 = New System.Windows.Forms.MenuItem
			Me.mnuSetFont = New System.Windows.Forms.MenuItem
			Me.mnuSelectAll = New System.Windows.Forms.MenuItem
			Me.menuItem2 = New System.Windows.Forms.MenuItem
			Me.menuItem5 = New System.Windows.Forms.MenuItem
			Me.saveFileDialog1 = New System.Windows.Forms.SaveFileDialog
			Me.textBoxNormal = New System.Windows.Forms.TextBox
			Me.mnuDelete = New System.Windows.Forms.MenuItem
			Me.mnuEdit = New System.Windows.Forms.MenuItem
			Me.mainMenu1 = New System.Windows.Forms.MainMenu(Me.components)
			Me.MenuItem1 = New System.Windows.Forms.MenuItem
			Me.mnu_showtext = New System.Windows.Forms.MenuItem
			Me.mnu_showmail = New System.Windows.Forms.MenuItem
			Me.mnu_showChat = New System.Windows.Forms.MenuItem
			Me.fontDialog1 = New System.Windows.Forms.FontDialog
			Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog
			Me.printDialog1 = New System.Windows.Forms.PrintDialog
			Me.textBoxEmail = New System.Windows.Forms.TextBox
			Me.textBoxChat = New System.Windows.Forms.TextBox
			Me.chatInput = New System.Windows.Forms.TextBox
			Me.timerChat = New System.Windows.Forms.Timer(Me.components)
			Me.StatusStripMain = New System.Windows.Forms.StatusStrip
			Me.sslCurrentChat = New System.Windows.Forms.ToolStripStatusLabel
			Me.StatusStripMain.SuspendLayout
			Me.SuspendLayout
			'
			'mnuPaste
			'
			Me.mnuPaste.Index = 4
			Me.mnuPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV
			Me.mnuPaste.Text = "&Paste"
			'
			'mnuSetFontColor
			'
			Me.mnuSetFontColor.Index = 1
			Me.mnuSetFontColor.Text = "Set Font C&olor..."
			'
			'MnuTimeDate
			'
			Me.MnuTimeDate.Index = 8
			Me.MnuTimeDate.Shortcut = System.Windows.Forms.Shortcut.F5
			Me.MnuTimeDate.Text = "&Time/Date"
			'
			'mnuSaveAs
			'
			Me.mnuSaveAs.Index = 3
			Me.mnuSaveAs.Text = "Save &As..."
			'
			'mnuSetBackCol
			'
			Me.mnuSetBackCol.Index = 2
			Me.mnuSetBackCol.Text = "Set &Background Color..."
			'
			'mnuNew
			'
			Me.mnuNew.Index = 0
			Me.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
			Me.mnuNew.Text = "&New"
			'
			'mnuAbout
			'
			Me.mnuAbout.Index = 0
			Me.mnuAbout.Text = "&About"
			'
			'mnuCopy
			'
			Me.mnuCopy.Index = 3
			Me.mnuCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC
			Me.mnuCopy.Text = "&Copy"
			'
			'mnuExit
			'
			Me.mnuExit.Index = 10
			Me.mnuExit.Shortcut = System.Windows.Forms.Shortcut.AltF4
			Me.mnuExit.Text = "E&xit"
			'
			'mnuUndo
			'
			Me.mnuUndo.Index = 0
			Me.mnuUndo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ
			Me.mnuUndo.Text = "&Undo"
			'
			'menuItem12
			'
			Me.menuItem12.Index = 9
			Me.menuItem12.Text = "-"
			'
			'mnuPrint
			'
			Me.mnuPrint.Index = 8
			Me.mnuPrint.Shortcut = System.Windows.Forms.Shortcut.CtrlP
			Me.mnuPrint.Text = "&Print"
			'
			'mnuFile
			'
			Me.mnuFile.Index = 0
			Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuNew, Me.mnuOpen, Me.mnuSave, Me.mnuSaveAs, Me.menuItem9, Me.mnu_Settings, Me.MenuItem4, Me.mnuPageSetUp, Me.mnuPrint, Me.menuItem12, Me.mnuExit})
			Me.mnuFile.Text = "&File"
			'
			'mnuOpen
			'
			Me.mnuOpen.Index = 1
			Me.mnuOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
			Me.mnuOpen.Text = "&Open..."
			'
			'mnuSave
			'
			Me.mnuSave.Index = 2
			Me.mnuSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
			Me.mnuSave.Text = "&Save"
			'
			'menuItem9
			'
			Me.menuItem9.Index = 4
			Me.menuItem9.Text = "-"
			'
			'mnu_Settings
			'
			Me.mnu_Settings.Index = 5
			Me.mnu_Settings.Text = "S&ettings"
			'
			'MenuItem4
			'
			Me.MenuItem4.Index = 6
			Me.MenuItem4.Text = "-"
			'
			'mnuPageSetUp
			'
			Me.mnuPageSetUp.Index = 7
			Me.mnuPageSetUp.Text = "Page Se&tup..."
			'
			'mnuWordWrap
			'
			Me.mnuWordWrap.Index = 9
			Me.mnuWordWrap.Text = "&Word Wrap"
			'
			'mnuCut
			'
			Me.mnuCut.Index = 2
			Me.mnuCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX
			Me.mnuCut.Text = "C&ut"
			'
			'menuItem7
			'
			Me.menuItem7.Index = 4
			Me.menuItem7.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuAbout})
			Me.menuItem7.Text = "&Help"
			'
			'menuItem6
			'
			Me.menuItem6.Index = 2
			Me.menuItem6.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuSetFont, Me.mnuSetFontColor, Me.mnuSetBackCol})
			Me.menuItem6.Text = "&Tools"
			'
			'mnuSetFont
			'
			Me.mnuSetFont.Index = 0
			Me.mnuSetFont.Text = "Set &Font..."
			'
			'mnuSelectAll
			'
			Me.mnuSelectAll.Index = 7
			Me.mnuSelectAll.Shortcut = System.Windows.Forms.Shortcut.CtrlA
			Me.mnuSelectAll.Text = "Select &All"
			'
			'menuItem2
			'
			Me.menuItem2.Index = 1
			Me.menuItem2.Text = "-"
			'
			'menuItem5
			'
			Me.menuItem5.Index = 6
			Me.menuItem5.Text = "-"
			'
			'saveFileDialog1
			'
			Me.saveFileDialog1.CheckFileExists = true
			Me.saveFileDialog1.DefaultExt = "txt"
			Me.saveFileDialog1.FileName = "doc1"
			Me.saveFileDialog1.Filter = "Text files (*.txt)|*.txt"
			Me.saveFileDialog1.RestoreDirectory = true
			'
			'textBoxNormal
			'
			Me.textBoxNormal.Location = New System.Drawing.Point(0, 5)
			Me.textBoxNormal.Multiline = true
			Me.textBoxNormal.Name = "textBoxNormal"
			Me.textBoxNormal.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
			Me.textBoxNormal.Size = New System.Drawing.Size(100, 20)
			Me.textBoxNormal.TabIndex = 0
			'
			'mnuDelete
			'
			Me.mnuDelete.Index = 5
			Me.mnuDelete.Shortcut = System.Windows.Forms.Shortcut.Del
			Me.mnuDelete.Text = "De&lete"
			'
			'mnuEdit
			'
			Me.mnuEdit.Index = 1
			Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuUndo, Me.menuItem2, Me.mnuCut, Me.mnuCopy, Me.mnuPaste, Me.mnuDelete, Me.menuItem5, Me.mnuSelectAll, Me.MnuTimeDate, Me.mnuWordWrap})
			Me.mnuEdit.Text = "&Edit"
			'
			'mainMenu1
			'
			Me.mainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuEdit, Me.menuItem6, Me.MenuItem1, Me.menuItem7})
			'
			'MenuItem1
			'
			Me.MenuItem1.Index = 3
			Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnu_showtext, Me.mnu_showmail, Me.mnu_showChat})
			Me.MenuItem1.Text = "&View"
			'
			'mnu_showtext
			'
			Me.mnu_showtext.Index = 0
			Me.mnu_showtext.Shortcut = System.Windows.Forms.Shortcut.F1
			Me.mnu_showtext.Text = "text"
			'
			'mnu_showmail
			'
			Me.mnu_showmail.Index = 1
			Me.mnu_showmail.Shortcut = System.Windows.Forms.Shortcut.F2
			Me.mnu_showmail.Text = "mail"
			'
			'mnu_showChat
			'
			Me.mnu_showChat.Index = 2
			Me.mnu_showChat.Shortcut = System.Windows.Forms.Shortcut.F3
			Me.mnu_showChat.Text = "chat"
			'
			'openFileDialog1
			'
			Me.openFileDialog1.DefaultExt = "*.txt"
			Me.openFileDialog1.Filter = "Text files (*.txt)|*.txt"
			Me.openFileDialog1.RestoreDirectory = true
			Me.openFileDialog1.Title = "Open File"
			'
			'textBoxEmail
			'
			Me.textBoxEmail.Location = New System.Drawing.Point(0, 31)
			Me.textBoxEmail.Multiline = true
			Me.textBoxEmail.Name = "textBoxEmail"
			Me.textBoxEmail.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
			Me.textBoxEmail.Size = New System.Drawing.Size(100, 20)
			Me.textBoxEmail.TabIndex = 1
			'
			'textBoxChat
			'
			Me.textBoxChat.Location = New System.Drawing.Point(0, 57)
			Me.textBoxChat.Multiline = true
			Me.textBoxChat.Name = "textBoxChat"
			Me.textBoxChat.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
			Me.textBoxChat.Size = New System.Drawing.Size(100, 20)
			Me.textBoxChat.TabIndex = 2
			'
			'chatInput
			'
			Me.chatInput.BorderStyle = System.Windows.Forms.BorderStyle.None
			Me.chatInput.Location = New System.Drawing.Point(0, 83)
			Me.chatInput.Multiline = true
			Me.chatInput.Name = "chatInput"
			Me.chatInput.Size = New System.Drawing.Size(100, 20)
			Me.chatInput.TabIndex = 3
			'
			'timerChat
			'
			Me.timerChat.Interval = 500
			'
			'StatusStripMain
			'
			Me.StatusStripMain.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.sslCurrentChat})
			Me.StatusStripMain.Location = New System.Drawing.Point(0, 439)
			Me.StatusStripMain.Name = "StatusStripMain"
			Me.StatusStripMain.Size = New System.Drawing.Size(640, 22)
			Me.StatusStripMain.TabIndex = 4
			'
			'sslCurrentChat
			'
			Me.sslCurrentChat.Name = "sslCurrentChat"
			Me.sslCurrentChat.Size = New System.Drawing.Size(0, 17)
			'
			'Form1
			'
			Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
			Me.ClientSize = New System.Drawing.Size(640, 461)
			Me.Controls.Add(Me.chatInput)
			Me.Controls.Add(Me.textBoxChat)
			Me.Controls.Add(Me.textBoxEmail)
			Me.Controls.Add(Me.textBoxNormal)
			Me.Controls.Add(Me.StatusStripMain)
			Me.Icon = CType(resources.GetObject("$this.Icon"),System.Drawing.Icon)
			Me.Menu = Me.mainMenu1
			Me.Name = "Form1"
			Me.Text = "Notepad"
			AddHandler FormClosing, AddressOf Me.Form1FormClosing
			Me.StatusStripMain.ResumeLayout(false)
			Me.StatusStripMain.PerformLayout
			Me.ResumeLayout(false)
			Me.PerformLayout
		End Sub	'InitializeComponent

#End Region

#Region " Form Events "

		Protected Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
			textBoxNormal.Width = Me.Width - 10
			textBoxNormal.Height = Me.Height - 55
			textBoxEmail.Width = Me.Width - 10
			textBoxEmail.Height = Me.Height - 55
			textBoxChat.Width = Me.Width - 10
			textBoxChat.Height = Me.Height - 55	' - chatInput.Height
			chatInput.Width = Me.Width - 35
			If Me.WindowState = FormWindowState.Minimized AndAlso Not _chat Is Nothing AndAlso _chat.IsLoggedIn AndAlso _chat.Status = Contact.ContactStatus.Active Then
				_chat.SetStatus(Contact.ContactStatus.Idle)
			ElseIf Not _chat Is Nothing AndAlso _chat.IsLoggedIn AndAlso _chat.Status = Contact.ContactStatus.Idle Then
				_chat.SetStatus(Contact.ContactStatus.Active)
			End If
		End Sub	'Form1_Resize

		Protected Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
			'check for unsaved data
			If checkUnSavedData() = False Then
				e.Cancel = True
				Return
			End If
			SaveSettings() 'Save setting to registry
		End Sub	'Form1_Closing

		Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
			'MessageBox.Show(settings.FileLocation)
			Me.Top = _settings.Top
			Me.Left = _settings.Left
			If _settings.Width > 0 Then Me.Width = _settings.Width
			If _settings.Height > 0 Then Me.Height = _settings.Height 
			If Not _settings.HaveLoginInfo Then
				openSettings()
			End If
		End Sub
		
		Sub Form1FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
			If Not e.Cancel Then
				_settings.Top = Me.Top
				_settings.Left = Me.Left
				_settings.Width = Me.Width
				_settings.Height = Me.Height 
				_settings.Save()
			End If
		End Sub

#End Region

#Region " Menus "

		Protected Sub mnuNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuNew.Click
			'check for unsaved data
			If checkUnSavedData() Then
				textBoxNormal.Clear() 'textBox1.Text="";			
				_textChanged = False
				_filePath = "Untitled"
				setTitle(_filePath)
			End If
		End Sub	'mnuNew_Click

		Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
			'todo
		End Sub

		Protected Sub mnuSaveAs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSaveAs.Click
			'Save File
			SaveFile()
		End Sub	'mnuSaveAs_Click

		Protected Sub mnuOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
			If checkUnSavedData() Then
				openFileDialog1.ShowDialog()
				Try
					If File.Exists(openFileDialog1.FileName) Then
						_filePath = openFileDialog1.FileName
						setTitle(_filePath)
						textBoxNormal.Text = File.ReadAllText(_filePath)
					End If
					textBoxNormal.SelectionStart = 0
					textBoxNormal.SelectionLength = 0
					mnu_showtext.PerformClick()
					_textChanged = False
				Catch
				End Try
			End If 'Cancel Pressed
		End Sub	'mnuOpen_Click

		Protected Sub mnuPaste_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
			'Using Clipboard complex....U can use Paste method-One line statement
			' textBox1.Paste();
			'******Using ClipBoard***************
			'Declare an IDataObject to hold the data returned from the clipboard.
			'Then retrieve the data from the clipboard.
			Dim iData As IDataObject = Clipboard.GetDataObject()
			'Determine whether the data is in a format you can use.
			If iData.GetDataPresent(DataFormats.Text) Then
				'Yes it is, so display it in a text box.			
				getSelectedTextBox().SelectedText = CType(iData.GetData(DataFormats.Text), [String])
			End If
		End Sub	'mnuPaste_Click

		Protected Sub mnuCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
			'Clipboard.SetDataObject(getSelectedTextBox(), True)
			Clipboard.SetText(getSelectedTextBox().SelectedText)
		End Sub	'mnuCopy_Click

		Protected Sub mnuPageSetUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPageSetUp.Click
			Dim docPrn As New PrintDocument
			docPrn.DocumentName = getSelectedTextBox().Text
			pageSetupDialog1.Document = docPrn
			pageSetupDialog1.ShowDialog()
		End Sub	'mnuPageSetUp_Click

		Protected Sub mnuSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSave.Click
			SaveFile()
		End Sub	'mnuSave_Click

		Protected Sub mnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuExit.Click
			'check for unsaved data
			If checkUnSavedData() Then
				Me.Close()
			End If
		End Sub	'mnuExit_Click

		Protected Sub mnuPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPrint.Click

			printDialog1.AllowPrintToFile = True
			Dim docPrn As New PrintDocument

			docPrn.DocumentName = getSelectedTextBox().Text

			printDialog1.Document = docPrn
			If printDialog1.ShowDialog() = DialogResult.OK Then
				Try
					docPrn.Print()
				Catch
				End Try
			End If
		End Sub	'mnuPrint_Click

		Protected Sub mnuSetBackCol_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSetBackCol.Click
			colorDialog1.ShowDialog()

			getSelectedTextBox().BackColor = colorDialog1.Color

		End Sub	'mnuSetBackCol_Click

		Protected Sub mnuFontColor_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSetFontColor.Click
			colorDialog1.ShowDialog()
			getSelectedTextBox().ForeColor = colorDialog1.Color
		End Sub	'mnuFontColor_Click

		Protected Sub mnuSetFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSetFont.Click
			fontDialog1.ShowDialog()
			getSelectedTextBox().Font = fontDialog1.Font
		End Sub	'mnuSetFont_Click

		Protected Sub mnuWordWrap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWordWrap.Click
			mnuWordWrap.Checked = Not mnuWordWrap.Checked

			If mnuWordWrap.Checked Then
				textBoxNormal.WordWrap = True
				textBoxNormal.ScrollBars = ScrollBars.Vertical
				textBoxEmail.WordWrap = True
				textBoxEmail.ScrollBars = ScrollBars.Vertical
				textBoxChat.WordWrap = True
				textBoxChat.ScrollBars = ScrollBars.Vertical
			Else
				textBoxNormal.WordWrap = False
				textBoxNormal.ScrollBars = ScrollBars.Both
				textBoxEmail.WordWrap = False
				textBoxEmail.ScrollBars = ScrollBars.Both
				textBoxChat.WordWrap = False
				textBoxChat.ScrollBars = ScrollBars.Both
			End If

		End Sub	'mnuWordWrap_Click


		Protected Sub MnuTimeDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MnuTimeDate.Click
			insertTextIntoTextBox(getSelectedTextBox(), DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString())
		End Sub	'MnuTimeDate_Click

		Private Sub insertTextIntoTextBox(ByRef txtBox As TextBox, ByVal txtToInsert As String)
			Dim sb As StringBuilder = New StringBuilder()
			Dim start As Integer = txtBox.SelectionStart
			sb.Append(txtBox.Text)
			sb.Insert(start, txtToInsert, 1)
			txtBox.Text = sb.ToString()
			txtBox.SelectionStart = start + txtToInsert.Length
		End Sub

		Protected Sub mnuSelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSelectAll.Click
			getSelectedTextBox().SelectAll()
		End Sub	'mnuSelectAll_Click

		Protected Sub mnuDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDelete.Click
			getSelectedTextBox().SelectedText = ""
		End Sub	'mnuDelete_Click

		Protected Sub mnuCut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuCut.Click
			getSelectedTextBox().Cut()
		End Sub	'mnuCut_Click

		Protected Sub mnuUndo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuUndo.Click
			getSelectedTextBox().Undo()
		End Sub	'mnuUndo_Click

		Private Sub mnu_showmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_showmail.Click
			showMail()
		End Sub

		Private Sub mnu_showtext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_showtext.Click
			_selectedTextBox = textBoxes.normal
			toggleVisibility(textBoxNormal, mnu_showtext)
			setTitle(_filePath)
		End Sub

		Private Sub mnu_showChat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_showChat.Click
			_selectedTextBox = textBoxes.chat
			toggleVisibility(textBoxChat, mnu_showChat)
			setTitle(_filePath)

			If _chatIsOn AndAlso _chat Is Nothing Then
				_chat = New Chat(_settings.Server, _settings.Port, _settings.ConnectServer, _settings.UserName, _settings.Password, _settings.Resource, _settings.UseSSL)
				AddHandler _chat.OnChatResponse, AddressOf chat_OnChatResponse
				AddHandler _chat.Contacts.OnContactStatusChanged, AddressOf contact_OnStatusChange
				_chat.Login()
			End If
		End Sub

#End Region

#Region " Notepad Save File/Settings Functions "
		Private Function SaveFile() As Boolean
			Try
				If _filePath = "Untitled" Then
					saveFileDialog1.CheckFileExists = False
					Dim dlgR As DialogResult = saveFileDialog1.ShowDialog()
					If dlgR = Windows.Forms.DialogResult.Cancel Then
						'Cancel
						_textChanged = True
						Return False
					End If
					_filePath = saveFileDialog1.FileName
				End If

				File.WriteAllText(_filePath, getSelectedTextBox().Text)

				setTitle(_filePath)
				_textChanged = False
				If _filePath = _settings.FileLocation Then _settings.Refresh()
				Return True
			Catch
			End Try
		End Function 'SaveFile


		Private Function checkUnSavedData() As Boolean
			If _textChanged = True Then
				Dim dlgRes As DialogResult
				dlgRes = MessageBox.Show("The text in the file has changed. Do you want to save changes?", "Notepad", MessageBoxButtons.YesNoCancel)

				Select Case dlgRes
					Case Windows.Forms.DialogResult.Yes

						Return SaveFile()
					Case Windows.Forms.DialogResult.No
						_textChanged = False
						Return True
					Case Windows.Forms.DialogResult.Cancel
						Return False
				End Select
			End If
			Return True
		End Function 'checkUnSavedData

		Private Sub SaveSettings()
			'Note I am not saving all properties of Font
			Try
				'Save final setting of notepad			
				Dim reKSubKey As RegistryKey = Registry.LocalMachine
				reKSubKey.CreateSubKey("NotepadVB")

				'Font related
				reKSubKey.SetValue("FontName", textBoxNormal.Font.FontFamily.GetName(0))
				reKSubKey.SetValue("FontSize", Convert.ToString(textBoxNormal.Font.Size))

				'Fore Color
				reKSubKey.SetValue("ForeCol", Convert.ToString(textBoxNormal.ForeColor.ToArgb()))

				'Back Col
				reKSubKey.SetValue("BackCol", Convert.ToString(textBoxNormal.BackColor.ToArgb()))
			Catch
			End Try
		End Sub	'SaveSettings

		Private Sub GetSettings()
			'Get last saved setting
			Try
				Dim reK As RegistryKey = Registry.LocalMachine
				reK.OpenSubKey("NotepadVB")

				textBoxNormal.Font = New System.Drawing.Font(reK.GetValue("FontName").ToString(), Convert.ToSingle(reK.GetValue("FontSize")))
				textBoxNormal.ForeColor = System.Drawing.Color.FromArgb(Convert.ToInt32(reK.GetValue("ForeCol")))
				textBoxNormal.BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(reK.GetValue("BackCol")))
			Catch
			End Try
		End Sub	'GetSettings

#End Region

#Region " Drag/Drop Events for the Chat Window "

		Private Sub textBoxChat_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles textBoxChat.DragEnter
			If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop) Then
				e.Effect = DragDropEffects.Link
			End If
		End Sub

		Private Sub textBoxChat_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles textBoxChat.DragDrop
			If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop) Then
				Dim filePaths As String() = e.Data.GetData(System.Windows.Forms.DataFormats.FileDrop)
				For Each file As String In filePaths
					Me.SetChatMessageText(file)
				Next
			End If
		End Sub

#End Region

		Protected Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBoxNormal.TextChanged
			_textChanged = True
		End Sub	'textBox1_TextChanged

		Private Sub getMail()
			If _emailIsOn AndAlso _settings.HaveLoginInfo Then
				Dim uid As String = _settings.UserName & "@" & _settings.Server
				Dim pwd As String = _settings.Password


				'** xmldoc testing
				Dim doc As Xml.XmlDocument = getXML(uid, pwd)
				Dim nodeList As Xml.XmlNodeList
				Dim mgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(doc.NameTable)
				mgr.AddNamespace("atom", _settings.AtomNamespace)
				Dim nodeFullCount As Xml.XmlNode = doc.SelectSingleNode("//atom:fullcount", mgr)

				If Not IsNothing(nodeFullCount) Then
					_mailCount = Convert.ToInt32(nodeFullCount.InnerText)
				End If

				'Write out the entries
				nodeList = doc.SelectNodes("//atom:entry", mgr)
				textBoxEmail.Text = String.Empty
				Dim title As String
				Dim summary As String
				Dim modified As String
				Dim author As String
				Dim modifiedDate As DateTime
				Dim node As Xml.XmlNode
				Dim mailBody As StringBuilder = New StringBuilder()

				For Each node In nodeList
					'title = ""
					'summary = ""
					'modified = ""
					title = node.SelectSingleNode("atom:title", mgr).InnerText
					summary = node.SelectSingleNode("atom:summary", mgr).InnerText
					modified = node.SelectSingleNode("atom:modified", mgr).InnerText
					author = node.SelectSingleNode("atom:author", mgr).InnerText
					If modified <> "" Then
						modifiedDate = Convert.ToDateTime(modified).ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss G\MT")
					End If
					mailBody.Append(modifiedDate.ToLocalTime.ToString() & " " & title & " " & author & _nl & summary & _nl & _nl)
				Next

				mailBody = mailBody.Replace("&#39;", "'")
				mailBody = mailBody.Replace("&hellip;", "...")
				textBoxEmail.Text = mailBody.ToString()
				textBoxEmail.SelectionStart = textBoxEmail.Text.Length
				textBoxEmail.SelectionLength = 0
				setTitle(_mailCount.ToString())
				'textBox1.Text = doc.OuterXml

			End If
		End Sub

		Private Sub setTitle(ByVal value As String)
			Me.Text = value & " - Notepad"
		End Sub

		Private Function getXML(ByVal uid As String, ByVal pwd As String) As XmlDocument

			Dim req As HttpWebRequest = WebRequest.Create(_settings.MailFeed)

			req.Method = "GET"
			req.Credentials = New NetworkCredential(uid, pwd)
			Dim response As XmlDocument = New XmlDocument

			Dim resp As HttpWebResponse

			resp = CType(req.GetResponse(), HttpWebResponse)

			'todo: error handling
			If resp.StatusCode = HttpStatusCode.OK Then
				Dim reader As XmlTextReader = New XmlTextReader(resp.GetResponseStream())
				response.Load(reader)
				reader.Close()
			End If

			resp.Close()
			Return response

		End Function

		Private Sub showMail()
			_selectedTextBox = textBoxes.gmail
			toggleVisibility(textBoxEmail, mnu_showmail)
			setTitle(_mailCount.ToString())
			textBoxEmail.Text = "get"
			getMail()
		End Sub

		Private Sub openSettings()
			Dim o As StreamReader = File.OpenText(_settings.FileLocation)
			textBoxNormal.Text = o.ReadToEnd()
			textBoxNormal.SelectionStart = 0
			textBoxNormal.SelectionLength = 0
			_textChanged = False
			saveFileDialog1.FileName = _settings.FileLocation
			mnu_showtext_Click(mnu_showtext, New EventArgs())
			o.Close()
			o.Dispose()
		End Sub

		Private Function getSelectedTextBox() As TextBox
			Dim selectedTextBox As TextBox = textBoxNormal
			Select Case _selectedTextBox
				Case textBoxes.gmail
					selectedTextBox = textBoxEmail
				Case textBoxes.chat
					selectedTextBox = textBoxChat
				Case textBoxes.normal
					selectedTextBox = textBoxNormal
			End Select
			Return selectedTextBox
		End Function

		Private Sub toggleVisibility(ByRef txtBox As TextBox, ByRef menu As System.Windows.Forms.MenuItem)
			textBoxNormal.Visible = False
			textBoxEmail.Visible = False
			textBoxChat.Visible = False
			chatInput.Visible = False
			sslCurrentChat.Visible = False

			If txtBox.Name.ToLower = "textboxchat" Then sslCurrentChat.Visible = True

			txtBox.Visible = True

			mnu_showmail.Checked = False
			mnu_showtext.Checked = False
			mnu_showChat.Checked = False
			If menu.MenuItems.Count = 0 Then menu.Checked = True
		End Sub

		Private Sub rosterItem_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
			'mnu_showChat_Click(sender, e)
			mnu_showChat.Shortcut = Shortcut.None
			mnu_showChat.PerformClick()
			For Each mi As MenuItem In mnu_showChat.MenuItems
				mi.Checked = False
				mi.Shortcut = Shortcut.None
			Next
			Dim currentRosterItem As MenuItem = CType(sender, MenuItem)
			currentRosterItem.Checked = True
			currentRosterItem.Shortcut = Shortcut.F3
			currentRosterItem.Enabled = True
			_currentContact = CType(currentRosterItem.Tag, Contact)
			sslCurrentChat.Text = _currentContact.ToString()
		End Sub

		Private Sub contact_OnStatusChange(ByVal sender As Object, ByVal status As Contact.ContactStatus)
			Dim c As Contact = CType(sender, Contact)
			Dim bFound As Boolean = False

			'TODO: Make this threadsafe? When trying to set the sslCurrentChat.Text when teh window is minimized, the program crashes.
			If Not _currentContact Is Nothing AndAlso c = _currentContact And Me.WindowState <> FormWindowState.Minimized Then sslCurrentChat.Text = c.ToString()
			If mnu_showChat.MenuItems.Count > 0 Then
				For Each mi As MenuItem In mnu_showChat.MenuItems
					If CType(mi.Tag, Contact) = c Then
						If c.Status = Contact.ContactStatus.Disconnected Then
							mnu_showChat.MenuItems.Remove(mi)
						Else
							mi.Text = c.ToString()
							mi.Tag = c
						End If
						bFound = True
						Exit For
					End If
				Next
				If Not bFound And c.Status <> Contact.ContactStatus.Disconnected Then AddRosterItemMenu(c)
				bFound = False
			Else
				AddRosterItemMenu(c)
			End If
		End Sub

		Private Sub chat_OnChatResponse(ByVal sender As Object, ByVal e As ChatResponseEventArgs)
			If e.Verbosity <= _settings.Verbosity Then
				Select Case e.Type
					Case ChatResponses.Chat
						SetChatMessageText(e.From & ": " & e.Message)
					Case ChatResponses.ErrorMessage
						SetChatMessageText("! Error: " & e.Message)
					Case ChatResponses.Status
						SetChatMessageText("-- " & e.Message)
					Case ChatResponses.XmlRead
						SetChatMessageText("--> " & e.Message)
					Case ChatResponses.XmlWrite
						SetChatMessageText("<-- " & e.Message)
				End Select
			End If
		End Sub

		Private Sub AddRosterItemMenu(ByVal item As Contact)
			Dim mnu_rosterItem As New MenuItem(item.ToString())
			mnu_rosterItem.RadioCheck = True
			mnu_rosterItem.Tag = item
			AddHandler mnu_rosterItem.Click, AddressOf rosterItem_OnClick
			mnu_showChat.MenuItems.Add(mnu_rosterItem)
		End Sub

		Private Sub SetChatMessageText(ByVal [text] As String)
			If Me.textBoxChat.InvokeRequired Then
				Dim d As New SetTextCallback(AddressOf SetChatMessageText)
				Me.Invoke(d, New Object() {[text]})
			Else
				'Want to make sure there is a new line at the top of the box
				'And, basically, I want a new line and I don't want to go through the code making sure I've put new lines in the right places
				'If String.IsNullOrEmpty(textBoxChat.Text) OrElse (textBoxChat.Text.EndsWith(_nl) = False And [text].StartsWith(_nl) = False) Then
				'	textBoxChat.AppendText(_nl)
				'End If
				If Not String.IsNullOrEmpty([text]) Then
					Dim idx As Integer = textBoxChat.SelectionStart
					If _isComposing Then textBoxChat.AppendText(_nl)
					textBoxChat.AppendText([text] & _nl)
					If _isComposing Then textBoxChat.SelectionStart = idx
				End If

			End If
		End Sub

		Private Sub timerChat_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerChat.Tick
			If _chatIsOn And _isComposing And Not _currentContact Is Nothing Then
				Dim ts As TimeSpan
				ts = Date.Now.Subtract(_lastChatTextChange)
				If ts.TotalSeconds >= 3 Then
					_chat.SetStatus(Contact.ContactStatus.Paused, _currentContact)
					_isComposing = False
					timerChat.Stop()
				End If
			End If
		End Sub

		Private Sub mnu_Settings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_Settings.Click
			openSettings()
		End Sub

		Private Sub textBoxChat_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textBoxChat.KeyDown, textBoxChat.KeyUp
			If e.KeyData = Keys.Enter AndAlso Not e.Shift AndAlso _chatStart <> -1 Then
				_chatEnd = textBoxChat.SelectionStart
				Debug.Print("Chat end set: " & _chatEnd.ToString())
				If _chatEnd > _chatStart Then
					Dim bufferStart As String = textBoxChat.Text.Substring(0, _chatStart)
					Dim bufferEnd As String = textBoxChat.Text.Substring(_chatEnd)
					Dim bufferChat As String = textBoxChat.Text.Substring(_chatStart, _chatEnd - _chatStart)

					If bufferChat.StartsWith(_nl) Then
						bufferChat = _nl & _settings.SelfLabel & bufferChat.Substring(_nl.Length)
					Else
						bufferChat = _settings.SelfLabel & bufferChat
					End If

					If timerChat.Enabled Then
						timerChat.Stop()
						_isComposing = False
					End If

					_chat.SendMessage(_currentContact, bufferChat.Substring(_settings.SelfLabel.Length))
					textBoxChat.Text = bufferStart & bufferEnd & bufferChat
					textBoxChat.SelectionStart = textBoxChat.Text.Length

					_chatStart = -1
					_chatEnd = -1
				End If
			ElseIf (e.KeyValue >= 33 And e.KeyValue <= 40) Or (e.KeyValue >= 13 And e.KeyValue <= 19) Or (e.KeyValue >= 44 And e.KeyValue <= 46) Or e.KeyValue = 8 Or e.KeyValue = 27 Or e.KeyValue = 91 Or e.KeyValue = 93 Then
				'Debug.Print("Control Pressed " & e.KeyCode & e.KeyCode.ToString())
			Else
				If _chatStart = -1 Then
					_chatStart = textBoxChat.SelectionStart
					Debug.Print("Chat start set: " & _chatStart.ToString())
				End If
				If Not _isComposing Then
					_isComposing = True
					_lastChatTextChange = Date.Now

					_chat.SetStatus(Contact.ContactStatus.Typing, _currentContact)

					timerChat.Enabled = True
					timerChat.Start()
				End If
			End If
		End Sub
		
	End Class 'Form1
End Namespace 'NotePad
